package jt.classic.system.workflow;

import jt.classic.system.ISystem;
import jt.classic.system.ISystemException;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.group.IGroup;
import jt.classic.system.user.IUser;

import org.limp.basework.console.XSystemConfig;

import org.limp.mine.DataTrimmer;

import java.sql.Connection;

import java.util.List;


/**
 * 系统组织机构管理中心超类
 *
 * @author bizkit
 *
 */
public abstract class AbstractWSupportUGCenter implements WSupportUGCenter {
    protected String defaultDS = (String) XSystemConfig.config()
                                                       .getProperty(XSystemConfig.XSYSTEM_DATASOURCE);
    protected boolean isdefault;
    protected boolean datasourceIsDefault;
    protected WSupport support;
    protected Connection conn;
    protected DataTrimmer trimmer;

    public void setSupport(WSupport support) throws ISystemException {
        if (support == null) {
            throw new ISystemException("support is NULL.");
        }

        this.support = support;

        if (WSupportCeneter.DEFAULTKEY.equals(support.ikey())) {
            this.isdefault = true;
            this.datasourceIsDefault = true;
        } else if (defaultDS.equals(support.idatasource())) {
            this.datasourceIsDefault = true;
        }
    }

    public void setConnection(Connection conn) {
        this.conn = conn;
        this.trimmer = new DataTrimmer(this.conn);
    }

    /**
     * 初始化数据库链接
     * @param conn 默认数据库链�?
     * @return Connection
     * @throws ISystemException
     */
    protected Connection getConnection() throws ISystemException {
        if (this.conn == null) {
            SystemDataSource ds = SystemDataSource.getInstance();

            try {
                if (this.isdefault ||
                        defaultDS.equals(this.support.idatasource())) {
                    this.conn = ds.getConnection();
                } else {
                    this.conn = ds.getConnection(this.support.idatasource());
                }

                this.trimmer = new DataTrimmer(this.conn);
            } catch (Exception ex) {
                throw new ISystemException("get connection from support[" +
                    this.support.iname() + "] datasource error.", ex);
            }
        } else {
            if (!datasourceIsDefault &&
                    !defaultDS.equals(this.support.idatasource())) {
                try {
                    SystemDataSource ds = SystemDataSource.getInstance();
                    this.conn = ds.getConnection(this.support.idatasource());
                    this.trimmer = new DataTrimmer(this.conn);
                } catch (Exception ex) {
                    this.conn = null;
                    this.trimmer = null;
                    throw new ISystemException("get connection from support[" +
                        this.support.iname() + "] datasource error.", ex);
                }
            }
        }

        return this.conn;
    }

    public boolean isdefault() {
        return this.isdefault;
    }

    public boolean datasourceIsDefault() {
        return this.datasourceIsDefault;
    }

    public void clean() {
        SystemDataSource.closeConnection(this.conn);
        this.trimmer = null;
    }
}
